Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Java版本设置工具 (PowerShell)" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

Write-Host "当前Java版本:" -ForegroundColor Yellow
try {
    $javaVersion = java -version 2>&1 | Select-String "version"
    Write-Host $javaVersion -ForegroundColor White
} catch {
    Write-Host "无法获取Java版本信息" -ForegroundColor Red
}
Write-Host ""

Write-Host "检查Java版本兼容性..." -ForegroundColor Yellow
try {
    $versionOutput = java -version 2>&1
    $versionMatch = $versionOutput | Select-String '"([^"]+)"'
    if ($versionMatch) {
        $currentVersion = $versionMatch.Matches[0].Groups[1].Value
        Write-Host "当前Java版本: $currentVersion" -ForegroundColor White
        
        # 提取主版本号
        $majorVersion = $currentVersion -replace '^"([0-9]+).*"$', '$1'
        $majorVersion = [int]$majorVersion
        
        if ($majorVersion -ge 21) {
            Write-Host "警告: 当前Java版本 $currentVersion 与Gradle 8.12不兼容" -ForegroundColor Red
            Write-Host "建议使用Java 11或Java 20" -ForegroundColor Yellow
            Write-Host ""
            Write-Host "解决方案:" -ForegroundColor Cyan
            Write-Host "1. 安装Java 11或Java 20" -ForegroundColor White
            Write-Host "2. 设置JAVA_HOME环境变量指向Java 11或Java 20" -ForegroundColor White
            Write-Host "3. 重新运行此脚本" -ForegroundColor White
            Write-Host ""
        } elseif ($majorVersion -ge 11) {
            Write-Host "Java版本 $currentVersion 与Gradle 8.12兼容" -ForegroundColor Green
            Write-Host ""
            Write-Host "设置JAVA_HOME环境变量..." -ForegroundColor Yellow
            try {
                $javaPath = (Get-Command java).Source
                $javaHome = Split-Path -Parent (Split-Path -Parent $javaPath)
                Write-Host "JAVA_HOME设置为: $javaHome" -ForegroundColor White
                Write-Host ""
                Write-Host "请在系统环境变量中设置JAVA_HOME为上述路径" -ForegroundColor Yellow
            } catch {
                Write-Host "无法自动获取Java路径" -ForegroundColor Red
            }
        } else {
            Write-Host "警告: Java版本过低，建议升级到Java 11或更高版本" -ForegroundColor Red
        }
    }
} catch {
    Write-Host "无法解析Java版本" -ForegroundColor Red
}

Write-Host ""
Write-Host "检查Gradle版本..." -ForegroundColor Yellow
try {
    $gradleVersion = .\gradlew.bat --version 2>&1 | Select-String "Gradle"
    Write-Host $gradleVersion -ForegroundColor White
} catch {
    Write-Host "无法获取Gradle版本信息" -ForegroundColor Red
}
Write-Host ""

Write-Host "建议的配置:" -ForegroundColor Cyan
Write-Host "- Java版本: 11 或 20" -ForegroundColor White
Write-Host "- Gradle版本: 8.12" -ForegroundColor White
Write-Host "- AGP版本: 8.4.0" -ForegroundColor White
Write-Host ""

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "设置完成" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

Read-Host "按任意键继续" 